<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\user\model\Content as ContentModel;
/**
 * 用户消息控制器
 * @package app\user\admin
 */
class Content extends Admin
{
    /**
     * 消息列表
     * @author 李洪波 <361951119@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data_list = ContentModel::where($this->getMap())
            ->order($this->getOrder('id DESC'))
            ->paginate();
        return ZBuilder::make('table')
            ->setTableName('admin_user_message')
            ->addRightButton('custom',['title'=>'view','icon'=>'fa fa-fw fa-search','href'=>url('send',['id'=>'__id__'])])
            ->addColumns([
                ['id', 'ID'],
                ['user_name', 'user name'],
                ['email', 'email'],
                ['phone', 'phone/address'],
                ['company', 'company'],
                ['is_replay', 'replay','','',[1=>'Yes',0=>'No']],
                ['create_time', 'create time'],
                ['right_button', 'operation', 'btn'],
            ])
            ->setColumnWidth([
                'id'=>30,
                'right_button'=>50,
                'email'=>150])
            ->setRowList($data_list)
            ->fetch();
    }


    public function send($id = 0){
        if ($id === 0) $this->error('missing parameter');
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['replay_content'] == '' && $this->error('Please fill in the reply content');
            $data['is_replay']==1 && $this->error('Do not resend!');
            $title=config('setting.company_replay_title');
            if(send_email($data['user_name'],$title,$data['replay_content'],$data['email'])){
                $update['is_replay']=1;
                $update['replay_content']=$data['replay_content'];
                if (ContentModel::update($update,['id'=>$id])) {
                    $this->success('reply success', 'index');
                } else {
                    $this->error('reply failed');
                }
            }else{
                $this->error('Email sending failed');
            }

        }
        $info=ContentModel::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'user_name', 'User Name','','','','disabled'],
                ['text', 'email', 'Email','','','','disabled'],
                ['text', 'phone', 'Phone','','','','disabled'],
                ['text', 'company', 'company','','','','disabled'],
                ['textarea','content','Content','','','disabled'],
                ['textarea','replay_content','Replay Content']
            ])
            ->addHidden('is_replay')
            ->setBtnTitle('submit', 'send')
            ->setFormData($info)
            ->fetch();
    }
}
